//
//  PersonalVipHeaderView.m
//  ChatForHot
//
//  Created by wr on 2018/10/11.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalVipHeaderView.h"
#import "PersonalVipHeadVIewCell.h"
#import "PersonalVipViewModel.h"
#import "PersonalVipInfoModel.h"
#import "PersonalVipSectionHeader.h"

@interface PersonalVipHeaderView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong) UICollectionView *HOTCHATcollectionView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) PersonalVipSectionHeader *HOTCHATheader;
@end

@implementation PersonalVipHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self HOTCHATdidInitialized];
    }
    return self;
}

- (void)HOTCHATdidInitialized {
    
    _dataSource = @[];
    
    self.HOTCHATheader = [[PersonalVipSectionHeader alloc] initWithFrame:CGRectZero];
    [self addSubview:self.HOTCHATheader];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(kScreenWidth / 3, 167);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.HOTCHATcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.HOTCHATcollectionView.backgroundColor = [UIColor whiteColor];
    self.HOTCHATcollectionView.showsVerticalScrollIndicator = NO;
    self.HOTCHATcollectionView.showsHorizontalScrollIndicator = NO;
    self.HOTCHATcollectionView.delegate = self;
    self.HOTCHATcollectionView.dataSource = self;
    self.HOTCHATcollectionView.scrollEnabled = NO;
    [self.HOTCHATcollectionView registerClass:[PersonalVipHeadVIewCell class] forCellWithReuseIdentifier:@"PersonalVipHeadVIewCell"];
    [self addSubview:self.HOTCHATcollectionView];
}

- (void)HOTCHATbind:(PersonalVipViewModel *)model {
    _model = model;
    _dataSource = model.dataArray;
    [self.HOTCHATheader HOTCHATbindbind:model];
    [self.HOTCHATcollectionView reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.HOTCHATheader.size = CGSizeMake(kScreenWidth, 44);
    self.HOTCHATheader.top = 0;
    self.HOTCHATheader.left = 0;
    
    self.HOTCHATcollectionView.size = CGSizeMake(kScreenWidth, 167);
    self.HOTCHATcollectionView.top = self.HOTCHATheader.bottom;
    self.HOTCHATcollectionView.left = 0;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PersonalVipHeadVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalVipHeadVIewCell" forIndexPath:indexPath];
    PersonalVipInfoModel *model = [self.dataSource objectOrNilAtIndex:indexPath.item];
    [cell HOTCHATbind:model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(HOTCHATheaderViewCellSelected:index:)]) {
        __block NSInteger oldSelectedIndex = 0;
        NSInteger newSelectedIndex = indexPath.item;
        [self.dataSource enumerateObjectsUsingBlock:^(PersonalVipInfoModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
            if (model.selected == YES) {
                oldSelectedIndex = idx;
                model.selected = NO;
            }
            if (idx == indexPath.item) {
                model.selected = YES;
            }
        }];
        
        if (oldSelectedIndex == newSelectedIndex) return;
        NSIndexPath *oldSelectedIndexPath = [NSIndexPath indexPathForItem:oldSelectedIndex inSection:0];
        NSIndexPath *newSelectedIndexPath = [NSIndexPath indexPathForItem:newSelectedIndex inSection:0];
        [self.HOTCHATcollectionView reloadItemsAtIndexPaths:@[oldSelectedIndexPath,newSelectedIndexPath]];
        [self.delegate HOTCHATheaderViewCellSelected:[self.dataSource objectAtIndex:indexPath.item] index:indexPath.item];
    }
}
@end
